import request from '@/utils/request'
import { Message } from 'element-ui'
import querystring from 'querystring'

const post = (url, params) => {

  let isNeedErrorToast = params._isNeedErrorToast || false;
  delete params._isNeedErrorToast;

  return request.post(url, params).then((data) => {
    return data
  }).catch((err) => {
    errorHandle(err, isNeedErrorToast)
    return Promise.reject(err)
  })
}


const get = (url, params) => {

  let isNeedErrorToast = params._isNeedErrorToast || false;
  delete params._isNeedErrorToast;

  let qs = querystring.stringify(params);
  return request.get(url + (qs ? '?' + qs : '')).then(data => {
    return data;
  }).catch(err => {
    errorHandle(err, isNeedErrorToast)
    return Promise.reject(err)
  })
}

function errorHandle(err, isNeedErrorToast) {
  if (isNeedErrorToast) {
    Message.error(err.showMsg || '系统繁忙，请稍候再试。');
  }
}

export default {
  post: post,
  get: get
}
